<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'sitecontroller.php';

class Usuario extends SiteController {
    
    function __construct(){
        parent::__construct();
        parent::auth();
    }
    
    public function lista(){
        if ($this->data['user']->getNivel() > 1){
            $this->data['usuarios'] = $this->doctrine->em->getRepository('Entities\Usuario')->findAll();
        }        
        
        $this->loadView('usuario/lista');
    }
    
    public function novo(){
        if ($this->data['user']->getNivel() > 1){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check[0]');
            $this->form_validation->set_rules('senha', 'Senha', 'required|trim');
            $this->form_validation->set_rules('nome', 'Nome', 'required|trim');

            if ($this->data['user']->getNivel() == 2){
                $this->form_validation->set_rules('nivel', 'N&iacute;vel', 'required|is_natural|less_than[3]');
            } else {
                $this->form_validation->set_rules('nivel', 'N&iacute;vel', 'required|is_natural|less_than[4]');
            }
            
            $this->form_validation->set_rules('telefone', 'Telefone', '');
            $this->form_validation->set_rules('cpf', 'Cpf', '');

            if ($this->form_validation->run()) {
                $usuario = new Entities\Usuario();
                $usuario->setEmail($this->input->post('email'));
                $usuario->setSenha(md5($this->input->post('senha')));
                $usuario->setNome($this->input->post('nome'));
                $usuario->setTelefone($this->input->post('telefone'));
                $usuario->setCpf($this->input->post('cpf'));
                $usuario->setNivel($this->input->post('nivel'));
                
                $this->doctrine->em->persist($usuario);
                $this->doctrine->em->flush();
                
                redirect('/usuario/ver/'.$usuario->getId());
            }
        }
        
        $this->loadView('usuario/novo');
    }
    
    public function ver($id){
        $this->data['usuario'] = $this->doctrine->em->getRepository('Entities\Usuario')->find($id);
        
        $chamados = array();
        $fluxos = $this->doctrine->em->getRepository('Entities\Fluxo')->findBy(array('usuarioResponsavel'=>$this->data['usuario']), array('data'=>'DESC'));
        foreach($fluxos as $fluxo){
            if (!in_array($fluxo->getChamado(), $chamados)){
                array_push($chamados, $fluxo->getChamado());
            }
        }
        foreach($this->doctrine->em->getRepository('Entities\Chamado')->findBy(array('status'=>0, 'usuario'=>$this->data['usuario'])) as $cham){
            if (!in_array($cham, $chamados)){
                array_push($chamados, $cham);
            }
        }
        $this->data['chamados'] = $chamados;        
        
        $this->loadView('usuario/ver');
    }
    
    public function editar(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check[1]');
        $this->form_validation->set_rules('senha', 'Senha', 'required|trim');
        $this->form_validation->set_rules('nome', 'Nome', 'required|trim');
        $this->form_validation->set_rules('telefone', 'Telefone', '');
        $this->form_validation->set_rules('cpf', 'Cpf', '');

        if ($this->form_validation->run()) {
            $this->data['user']->setEmail($this->input->post('email'));
            $this->data['user']->setSenha(md5($this->input->post('senha')));
            $this->data['user']->setNome($this->input->post('nome'));
            $this->data['user']->setTelefone($this->input->post('telefone'));
            $this->data['user']->setCpf($this->input->post('cpf'));

            $this->doctrine->em->flush();

            redirect('/usuario/ver/'.$this->data['user']->getId());
        }
        
        $this->loadView('usuario/editar');
    }
    
    public function email_check($email = '', $editar = 0) {
        $user = $this->doctrine->em->getRepository ( 'Entities\Usuario' )->findOneBy ( array (
            'email' => $this->input->post ( 'email' ) 
        ) );
        if (is_null ( $user )) {
            return TRUE;
        } elseif($editar == 1 && $user->getEmail() == $this->data['user']->getEmail()) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
}

